package com.dykj.platform.iadapter.utils;

import java.sql.Date;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DateUtil {

    //系统日志־
    private static Log log = LogFactory.getLog(DateUtil.class);

    public DateUtil() {
    }
    /**
     * 输入正确的身份证号码返回年龄
     * @param id 身份证号码
     * @return 年龄
     * @throws Exception 身份证号码不正确（长度不够）
     */
    public static int getAge(String id) throws Exception {
        int age = -1;
        int length = id.length();
        String birthday = "";
        if (length == 15) {
            birthday = id.substring(6, 8);
            birthday = "19" + birthday;
        } else if (length == 18)
            birthday = id.substring(6, 10);
        else
            throw new Exception("错误的身份证号");
        int currentYear = Calendar.getInstance().get(1);
        age = currentYear - (new Integer(birthday)).intValue();
        return age;
    }
    /**
     * 通过年龄获得生日
     * @param age 年龄
     * @return 生日年月日
     */
    public static Date getDateByAge(int age) {
        Calendar calendar = Calendar.getInstance(Locale.CHINESE);
        long current = calendar.getTimeInMillis();
        calendar.set(calendar.get(1) - age, calendar.get(2), calendar.get(5));
        return new Date(calendar.getTimeInMillis());
    }
    /**
     * 中国风格的时间显示
     * @param d
     * @return 如：1975年10月27日
     */
    public static String getChineseDate(java.util.Date d) {
        if (d == null) {
            return new String();
        } else {
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd", new DateFormatSymbols());
            String dtrDate = df.format(d);
            return dtrDate.substring(0, 4) + "年" + Integer.parseInt(dtrDate.substring(4, 6)) + "月" + Integer.parseInt(dtrDate.substring(6, 8)) + "日";
        }
    }
    /**
     * 获得当前日期字符串
     * @return 如：20050511
     */
    public static String getCurrentDate_String() {
        Calendar cal = Calendar.getInstance();
        String currentDate = null;
        java.util.Date d = cal.getTime();
        String currentYear = (new Integer(cal.get(1))).toString();
        String currentMonth = null;
        String currentDay = null;
        if (cal.get(2) < 9)
            currentMonth = "0" + (new Integer(cal.get(2) + 1)).toString();
        else
            currentMonth = (new Integer(cal.get(2) + 1)).toString();
        if (cal.get(5) < 10)
            currentDay = "0" + (new Integer(cal.get(5))).toString();
        else
            currentDay = (new Integer(cal.get(5))).toString();
        currentDate = currentYear + currentMonth + currentDay;
        return currentDate;
    }
    /**
     * 按指定形式显示时间
     * @param strFormat yyyy-mm-dd
     * @return 时间字符串
     */
    public static String getCurrentDate_String(String strFormat) {
        Calendar cal = Calendar.getInstance();
        String currentDate = null;
        java.util.Date d = cal.getTime();
        return getDate(d, strFormat);
    }

    public static int calBetweenTwoMonth(String dealMonth, String alterMonth) {
        int length = 0;
        if (dealMonth.length() != 6 || alterMonth.length() != 6) {
            length = -1;
        } else {
            int dealInt = Integer.parseInt(dealMonth);
            int alterInt = Integer.parseInt(alterMonth);
            if (dealInt < alterInt) {
                length = -2;
            } else {
                int dealYearInt = Integer.parseInt(dealMonth.substring(0, 4));
                int dealMonthInt = Integer.parseInt(dealMonth.substring(4, 6));
                int alterYearInt = Integer.parseInt(alterMonth.substring(0, 4));
                int alterMonthInt = Integer.parseInt(alterMonth.substring(4, 6));
                length = (dealYearInt - alterYearInt) * 12 + (dealMonthInt - alterMonthInt);
            }
        }
        return length;
    }

    public static int convertDateToYear(java.util.Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy", new DateFormatSymbols());
        return Integer.parseInt(df.format(date));
    }

    public static String convertDateToYearMonth(java.util.Date d) {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMM", new DateFormatSymbols());
        return df.format(d);
    }

    public static String convertDateToYearMonthDay(java.util.Date d) {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd", new DateFormatSymbols());
        return df.format(d);
    }

    public static int daysBetweenDates(java.util.Date newDate, java.util.Date oldDate) {
        int days = 0;
        Calendar calo = Calendar.getInstance();
        Calendar caln = Calendar.getInstance();
        calo.setTime(oldDate);
        caln.setTime(newDate);
        int oday = calo.get(6);
        int nyear = caln.get(1);
        for (int oyear = calo.get(1); nyear > oyear;) {
            calo.set(2, 11);
            calo.set(5, 31);
            days += calo.get(6);
            oyear++;
            calo.set(1, oyear);
        }

        int nday = caln.get(6);
        days = (days + nday) - oday;
        return days;
    }

    public static java.util.Date getDateBetween(java.util.Date date, int intBetween) {
        Calendar calo = Calendar.getInstance();
        calo.setTime(date);
        calo.add(5, intBetween);
        return calo.getTime();
    }

    public static String getDateBetween_String(java.util.Date date, int intBetween, String strFromat) {
        java.util.Date dateOld = getDateBetween(date, intBetween);
        return getDate(dateOld, strFromat);
    }

    public static String increaseYearMonth(String yearMonth) {
        int year = (new Integer(yearMonth.substring(0, 4))).intValue();
        int month = (new Integer(yearMonth.substring(4, 6))).intValue();
        if (++month <= 12 && month >= 10)
            return yearMonth.substring(0, 4) + (new Integer(month)).toString();
        if (month < 10)
            return yearMonth.substring(0, 4) + "0" + (new Integer(month)).toString();
        else
            return (new Integer(year + 1)).toString() + "0" + (new Integer(month - 12)).toString();
    }

    public static String increaseYearMonth(String yearMonth, int addMonth) {
        int year = (new Integer(yearMonth.substring(0, 4))).intValue();
        int month = (new Integer(yearMonth.substring(4, 6))).intValue();
        month += addMonth;
        year += month / 12;
        month %= 12;
        if (month <= 12 && month >= 10)
            return year + (new Integer(month)).toString();
        else
            return year + "0" + (new Integer(month)).toString();
    }

    public static String descreaseYearMonth(String yearMonth) {
        int year = (new Integer(yearMonth.substring(0, 4))).intValue();
        int month = (new Integer(yearMonth.substring(4, 6))).intValue();
        if (--month >= 10)
            return yearMonth.substring(0, 4) + (new Integer(month)).toString();
        if (month > 0 && month < 10)
            return yearMonth.substring(0, 4) + "0" + (new Integer(month)).toString();
        else
            return (new Integer(year - 1)).toString() + (new Integer(month + 12)).toString();
    }

    public static java.util.Date getCurrentDate() {
        Calendar cal = Calendar.getInstance();
        String currentDate = null;
        java.util.Date d = cal.getTime();
        return d;
    }

    public static String getCurrentYearMonth() {
        Calendar cal = Calendar.getInstance();
        String currentYear = (new Integer(cal.get(1))).toString();
        String currentMonth = null;
        if (cal.get(2) < 9)
            currentMonth = "0" + (new Integer(cal.get(2) + 1)).toString();
        else
            currentMonth = (new Integer(cal.get(2) + 1)).toString();
        return currentYear + currentMonth;
    }
    
    public static String getCurrentMonth() {
        Calendar cal = Calendar.getInstance();
        String currentYear = (new Integer(cal.get(1))).toString();
        String currentMonth = null;
        if (cal.get(2) < 9)
            currentMonth = "0" + (new Integer(cal.get(2) + 1)).toString();
        else
            currentMonth = (new Integer(cal.get(2) + 1)).toString();
        return currentMonth;
    }

    public static int getCurrentYear() {
        Calendar cal = Calendar.getInstance();
        int currentYear = cal.get(1);
        return currentYear;
    }

    public static java.util.Date stringToDate(String strDate, String oracleFormat) {
        if (strDate == null)
            return null;
        Hashtable h = new Hashtable();
        String javaFormat = new String();
        String s = oracleFormat.toLowerCase();
        if (s.indexOf("yyyy") != -1)
            h.put(new Integer(s.indexOf("yyyy")), "yyyy");
        else if (s.indexOf("yy") != -1)
            h.put(new Integer(s.indexOf("yy")), "yy");
        if (s.indexOf("mm") != -1)
            h.put(new Integer(s.indexOf("mm")), "MM");
        if (s.indexOf("dd") != -1)
            h.put(new Integer(s.indexOf("dd")), "dd");
        if (s.indexOf("hh24") != -1)
            h.put(new Integer(s.indexOf("hh24")), "HH");
        if (s.indexOf("mi") != -1)
            h.put(new Integer(s.indexOf("mi")), "mm");
        if (s.indexOf("ss") != -1)
            h.put(new Integer(s.indexOf("ss")), "ss");
        for (int intStart = 0; s.indexOf("-", intStart) != -1; intStart++) {
            intStart = s.indexOf("-", intStart);
            h.put(new Integer(intStart), "-");
        }

        for (int intStart = 0; s.indexOf("/", intStart) != -1; intStart++) {
            intStart = s.indexOf("/", intStart);
            h.put(new Integer(intStart), "/");
        }

        for (int intStart = 0; s.indexOf(" ", intStart) != -1; intStart++) {
            intStart = s.indexOf(" ", intStart);
            h.put(new Integer(intStart), " ");
        }

        for (int intStart = 0; s.indexOf(":", intStart) != -1; intStart++) {
            intStart = s.indexOf(":", intStart);
            h.put(new Integer(intStart), ":");
        }

        if (s.indexOf("年") != -1)
            h.put(new Integer(s.indexOf("年")), "年");
        if (s.indexOf("月") != -1)
            h.put(new Integer(s.indexOf("月")), "月");
        if (s.indexOf("日") != -1)
            h.put(new Integer(s.indexOf("日")), "日");
        if (s.indexOf("时") != -1)
            h.put(new Integer(s.indexOf("时")), "时");
        if (s.indexOf("分") != -1)
            h.put(new Integer(s.indexOf("分")), "分");
        if (s.indexOf("秒") != -1)
            h.put(new Integer(s.indexOf("秒")), "秒");
        int i = 0;
        while (h.size() != 0) {
            Enumeration e = h.keys();
            int n = 0;
            while (e.hasMoreElements()) {
                i = ((Integer) e.nextElement()).intValue();
                if (i >= n)
                    n = i;
            }
            String temp = (String) h.get(new Integer(n));
            h.remove(new Integer(n));
            javaFormat = temp + javaFormat;
        }
        SimpleDateFormat df = new SimpleDateFormat(javaFormat);
        java.util.Date myDate = new java.util.Date();
        try {
            myDate = df.parse(strDate);
        } catch (Exception e) {
            return null;
        }
        return myDate;
    }

    public static String dateToString(java.util.Date d, String format) {
        if (d == null)
            return "";
        Hashtable h = new Hashtable();
        String javaFormat = new String();
        String s = format.toLowerCase();
        if (s.indexOf("yyyy") != -1)
            h.put(new Integer(s.indexOf("yyyy")), "yyyy");
        else if (s.indexOf("yy") != -1)
            h.put(new Integer(s.indexOf("yy")), "yy");
        if (s.indexOf("mm") != -1)
            h.put(new Integer(s.indexOf("mm")), "MM");
        if (s.indexOf("dd") != -1)
            h.put(new Integer(s.indexOf("dd")), "dd");
        if (s.indexOf("hh24") != -1)
            h.put(new Integer(s.indexOf("hh24")), "HH");
        if (s.indexOf("mi") != -1)
            h.put(new Integer(s.indexOf("mi")), "mm");
        if (s.indexOf("ss") != -1)
            h.put(new Integer(s.indexOf("ss")), "ss");
        for (int intStart = 0; s.indexOf("-", intStart) != -1; intStart++) {
            intStart = s.indexOf("-", intStart);
            h.put(new Integer(intStart), "-");
        }

        for (int intStart = 0; s.indexOf("/", intStart) != -1; intStart++) {
            intStart = s.indexOf("/", intStart);
            h.put(new Integer(intStart), "/");
        }

        for (int intStart = 0; s.indexOf(" ", intStart) != -1; intStart++) {
            intStart = s.indexOf(" ", intStart);
            h.put(new Integer(intStart), " ");
        }

        for (int intStart = 0; s.indexOf(":", intStart) != -1; intStart++) {
            intStart = s.indexOf(":", intStart);
            h.put(new Integer(intStart), ":");
        }

        if (s.indexOf("年") != -1)
            h.put(new Integer(s.indexOf("年")), "年");
        if (s.indexOf("月") != -1)
            h.put(new Integer(s.indexOf("月")), "月");
        if (s.indexOf("日") != -1)
            h.put(new Integer(s.indexOf("日")), "日");
        if (s.indexOf("时") != -1)
            h.put(new Integer(s.indexOf("时")), "时");
        if (s.indexOf("分") != -1)
            h.put(new Integer(s.indexOf("分")), "分");
        if (s.indexOf("秒") != -1)
            h.put(new Integer(s.indexOf("秒")), "秒");
        int i = 0;
        while (h.size() != 0) {
            Enumeration e = h.keys();
            int n = 0;
            while (e.hasMoreElements()) {
                i = ((Integer) e.nextElement()).intValue();
                if (i >= n)
                    n = i;
            }
            String temp = (String) h.get(new Integer(n));
            h.remove(new Integer(n));
            javaFormat = temp + javaFormat;
        }
        SimpleDateFormat df = new SimpleDateFormat(javaFormat, new DateFormatSymbols());
        return df.format(d);
    }

    /**
     * 返回生日Date对象
     * @param year 1920
     * @param month 11
     * @param day 11
     * @return 生日Date对象
     */
    public static Date getBirth(String year, String month, String day) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        String req_year = year.trim();
        String req_month = month.trim();
        String req_day = day.trim();
        if (req_month.length() == 1) {
            req_month = "0" + req_month;
        }
        if (req_day.length() == 1) {
            req_day = "0" + req_day;
        }
        String birth = req_year + "-" + req_month + "-" + req_day;
        return new Date(stringToDate(birth, "yyyy-mm-dd").getTime());
    }

    /**
     * 
     * @param d
     * @param format
     * @return
     */
    public static String getDate(java.util.Date d, String format) {
        if (d == null)
            return "";
        Hashtable h = new Hashtable();
        String javaFormat = new String();
        String s = format.toLowerCase();
        if (s.indexOf("yyyy") != -1)
            h.put(new Integer(s.indexOf("yyyy")), "yyyy");
        else if (s.indexOf("yy") != -1)
            h.put(new Integer(s.indexOf("yy")), "yy");
        if (s.indexOf("mm") != -1)
            h.put(new Integer(s.indexOf("mm")), "MM");
        if (s.indexOf("dd") != -1)
            h.put(new Integer(s.indexOf("dd")), "dd");
        if (s.indexOf("hh24") != -1)
            h.put(new Integer(s.indexOf("hh24")), "HH");
        if (s.indexOf("mi") != -1)
            h.put(new Integer(s.indexOf("mi")), "mm");
        if (s.indexOf("ss") != -1)
            h.put(new Integer(s.indexOf("ss")), "ss");
        for (int intStart = 0; s.indexOf("-", intStart) != -1; intStart++) {
            intStart = s.indexOf("-", intStart);
            h.put(new Integer(intStart), "-");
        }

        for (int intStart = 0; s.indexOf("/", intStart) != -1; intStart++) {
            intStart = s.indexOf("/", intStart);
            h.put(new Integer(intStart), "/");
        }

        for (int intStart = 0; s.indexOf(" ", intStart) != -1; intStart++) {
            intStart = s.indexOf(" ", intStart);
            h.put(new Integer(intStart), " ");
        }

        for (int intStart = 0; s.indexOf(":", intStart) != -1; intStart++) {
            intStart = s.indexOf(":", intStart);
            h.put(new Integer(intStart), ":");
        }

        if (s.indexOf("年") != -1)
            h.put(new Integer(s.indexOf("年")), "年");
        if (s.indexOf("月") != -1)
            h.put(new Integer(s.indexOf("月")), "月");
        if (s.indexOf("日") != -1)
            h.put(new Integer(s.indexOf("日")), "日");
        if (s.indexOf("时") != -1)
            h.put(new Integer(s.indexOf("时")), "时");
        if (s.indexOf("分") != -1)
            h.put(new Integer(s.indexOf("分")), "分");
        if (s.indexOf("秒") != -1)
            h.put(new Integer(s.indexOf("秒")), "秒");
        int i = 0;
        while (h.size() != 0) {
            Enumeration e = h.keys();
            int n = 0;
            while (e.hasMoreElements()) {
                i = ((Integer) e.nextElement()).intValue();
                if (i >= n)
                    n = i;
            }
            String temp = (String) h.get(new Integer(n));
            h.remove(new Integer(n));
            javaFormat = temp + javaFormat;
        }
        SimpleDateFormat df = new SimpleDateFormat(javaFormat, new DateFormatSymbols());
        return df.format(d);
    }

    public static boolean yearMonthGreatEqual(String s1, String s2) {
        String temp1 = s1.substring(0, 4);
        String temp2 = s2.substring(0, 4);
        String temp3 = s1.substring(4, 6);
        String temp4 = s2.substring(4, 6);
        if (Integer.parseInt(temp1) > Integer.parseInt(temp2))
            return true;
        if (Integer.parseInt(temp1) == Integer.parseInt(temp2))
            return Integer.parseInt(temp3) >= Integer.parseInt(temp4);
        else
            return false;
    }

    public static boolean yearMonthGreater(String s1, String s2) {
        String temp1 = s1.substring(0, 4);
        String temp2 = s2.substring(0, 4);
        String temp3 = s1.substring(4, 6);
        String temp4 = s2.substring(4, 6);
        if (Integer.parseInt(temp1) > Integer.parseInt(temp2))
            return true;
        if (Integer.parseInt(temp1) == Integer.parseInt(temp2))
            return Integer.parseInt(temp3) > Integer.parseInt(temp4);
        else
            return false;
    }

    public static String getOracleFormatDateStr(java.util.Date d) {
        return getDate(d, "YYYY-MM-DD HH24:MI:SS");
    }

    public static String getLastDay(String term) {
        int getYear = Integer.parseInt(term.substring(0, 4));
        int getMonth = Integer.parseInt(term.substring(4, 6));
        String getLastDay = "";
        if (getMonth == 2) {
            if (getYear % 4 == 0 && getYear % 100 != 0 || getYear % 400 == 0)
                getLastDay = "29";
            else
                getLastDay = "28";
        } else if (getMonth == 4 || getMonth == 6 || getMonth == 9 || getMonth == 11)
            getLastDay = "30";
        else
            getLastDay = "31";
        return String.valueOf(getYear) + "年" + String.valueOf(getMonth) + "月" + getLastDay + "日";
    }

    public static String getMonthBetween(String strDateBegin, String strDateEnd) {
        try {
            String strOut;
            if (strDateBegin.equals("") || strDateEnd.equals("") || strDateBegin.length() != 6 || strDateEnd.length() != 6) {
                strOut = "";
            } else {
                int intMonthBegin = Integer.parseInt(strDateBegin.substring(0, 4)) * 12 + Integer.parseInt(strDateBegin.substring(4, 6));
                int intMonthEnd = Integer.parseInt(strDateEnd.substring(0, 4)) * 12 + Integer.parseInt(strDateEnd.substring(4, 6));
                strOut = String.valueOf(intMonthBegin - intMonthEnd);
            }
            return strOut;
        } catch (Exception e) {
            return "0";
        }
    }

    public static String getStrHaveAcross(String strDate) {
        try {
            return strDate.substring(0, 4) + "-" + strDate.substring(4, 6) + "-" + strDate.substring(6, 8);
        } catch (Exception e) {
            return strDate;
        }
    }

    public static String getFirstDayOfNextMonth() {
        String strToday = getCurrentDate_String();
        return increaseYearMonth(strToday.substring(0, 6)) + "01";
    }

    public static String getYearAndMonth(String yearMonth) {
        if (yearMonth == null)
            return "";
        String ym = yearMonth.trim();
        if (6 != ym.length()) {
            return ym;
        } else {
            String year = ym.substring(0, 4);
            String month = ym.substring(4);
            return year + "年" + month + "月";
        }
    }

    public static String month2YearMonth(String month) {
        String yearMonth = "";
        int smonth = 0;
        int year = 0;
        int rmonth = 0;
        if (month == null || "0".equals(month) || "".equals(month.trim()))
            return "0月";
        smonth = Integer.parseInt(month);
        year = smonth / 12;
        rmonth = smonth % 12;
        if (year > 0)
            yearMonth = year + "年";
        if (rmonth > 0)
            yearMonth = yearMonth + rmonth + "个月";
        return yearMonth;
    }

    public static String getYearMonthByMonth(String month) {
        if (month == null)
            return null;
        String ym = month.trim();
        if (6 != ym.length()) {
            return ym;
        } else {
            String year = ym.substring(0, 4);
            String month1 = ym.substring(4);
            return year + "年" + month + "月";
        }
    }

    public static java.util.Date increaseMonth(java.util.Date date, int intBetween) {
        Calendar calo = Calendar.getInstance();
        calo.setTime(date);
        calo.add(2, intBetween);
        return calo.getTime();
    }

    public static java.util.Date increaseYear(java.util.Date date, int intBetween) {
        Calendar calo = Calendar.getInstance();
        calo.setTime(date);
        calo.add(1, intBetween);
        return calo.getTime();
    }
}
